package cn.itsource.basic.aop;

import cn.itsource.basic.annotation.Repeat;
import cn.itsource.basic.config.exception.BusinessException;
import cn.itsource.basic.util.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
@Slf4j
public class RepeatAop {
    @Autowired
    private RedisService redisService;
    @Autowired
    private HttpServletRequest autowiredRequest;
    /**
     * 切点(何地)
     * @param repeat
     */
    @Pointcut("@annotation(repeat)")
    public void cut(Repeat repeat){

    }

    /**
     * 切面
     * 在执行方法之前
     * @param repeat
     */
    @Before("cut(repeat)")
    public void repeat(Repeat repeat){
        //获取sevlet对象
        String requestURI = autowiredRequest.getRequestURI();
        //获取剑值
        String time = redisService.getKeyStringValue(requestURI);
     //   log.info("存储的时间:{}",time);
        //判断
        if(StringUtils.isNotBlank(time)){
            throw new BusinessException("兄弟去打物质瑞");
        }
        redisService.setStringKeyAndValue(requestURI,"12345",repeat.value());


    }
}
